home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / gdb35src.zoo / dist-gdb / inflow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-12  |  14.8 KB  |  600 lines

  1. /* Low level interface to ptrace, for GDB when running under Unix.
  2.    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. GDB is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GDB is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GDB; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #ifndef atarist
  21. #define TTY_FD 0
  22. #else
  23. #define TTY_FD 1
  24. #endif
  25.  
  26. #include <stdio.h>
  27. #include "defs.h"
  28. #include "param.h"
  29. #include "frame.h"
  30. #include "inferior.h"
  31.  
  32. #ifdef atarist
  33. #include <mintbind.h>
  34. #endif
  35.  
  36. #ifdef USG
  37. #include <sys/types.h>
  38. #endif
  39.  
  40. /* Some USG-esque systems (some of which are BSD-esque enough so that USG
  41.    is not defined) want this header, and it won't do any harm.  */
  42. #include <fcntl.h>
  43.  
  44. #include <sys/param.h>
  45. #include <sys/dir.h>
  46. #include <signal.h>
  47.  
  48. #ifdef HAVE_TERMIO
  49. #include <termio.h>
  50. #undef TIOCGETP
  51. #define TIOCGETP TCGETA
  52. #undef TIOCSETN
  53. #define TIOCSETN TCSETA
  54. #undef TIOCSETP
  55. #define TIOCSETP TCSETAF
  56. #define TERMINAL struct termio
  57. #else
  58. #include <sys/ioctl.h>
  59. #include <fcntl.h>
  60. #include <sgtty.h>
  61. #define TERMINAL struct sgttyb
  62. #endif
  63.  
  64. #ifdef SET_STACK_LIMIT_HUGE
  65. #include <sys/time.h>
  66. #include <sys/resource.h>
  67. extern int original_stack_limit;
  68. #endif /* SET_STACK_LIMIT_HUGE */
  69.  
  70. extern int errno;
  71.  
  72. /* Nonzero if we are debugging an attached outside process
  73.    rather than an inferior.  */
  74.  
  75. int attach_flag;
  76.  
  77.  
  78. /* Record terminal status separately for debugger and inferior.  */
  79.  
  80. static TERMINAL sg_inferior;
  81. static TERMINAL sg_ours;
  82.  
  83. static int tflags_inferior;
  84. static int tflags_ours;
  85.  
  86. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  87. static struct tchars tc_inferior;
  88. static struct tchars tc_ours;
  89. #endif
  90.  
  91. #ifdef TIOCGLTC
  92. static struct ltchars ltc_inferior;
  93. static struct ltchars ltc_ours;
  94. #endif
  95.  
  96. #ifdef TIOCLGET
  97. static int lmode_inferior;
  98. static int lmode_ours;
  99. #endif
  100.  
  101. #ifdef TIOCGPGRP
  102. static int pgrp_inferior;
  103. static int pgrp_ours;
  104. #else
  105. static int (*sigint_ours) ();
  106. static int (*sigquit_ours) ();
  107. #endif /* TIOCGPGRP */
  108.  
  109. /* Copy of inferior_io_terminal when inferior was last started.  */
  110. static char *inferior_thisrun_terminal;
  111.  
  112. static void terminal_ours_1 ();
  113.  
  114. /* Nonzero if our terminal settings are in effect.
  115.    Zero if the inferior's settings are in effect.  */
  116. static int terminal_is_ours;
  117.  
  118. /* Initialize the terminal settings we record for the inferior,
  119.    before we actually run the inferior.  */
  120.  
  121. void
  122. terminal_init_inferior ()
  123. {
  124.   if (remote_debugging)
  125.     return;
  126.  
  127.   sg_inferior = sg_ours;
  128.   tflags_inferior = tflags_ours;
  129.  
  130. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  131.   tc_inferior = tc_ours;
  132. #endif
  133.  
  134. #ifdef TIOCGLTC
  135.   ltc_inferior = ltc_ours;
  136. #endif
  137.  
  138. #ifdef TIOCLGET
  139.   lmode_inferior = lmode_ours;
  140. #endif
  141.  
  142. #ifdef TIOCGPGRP
  143.   pgrp_inferior = inferior_pid;
  144. #endif /* TIOCGPGRP */
  145.  
  146.   terminal_is_ours = 1;
  147. }
  148.  
  149. /* Put the inferior's terminal settings into effect.
  150.    This is preparation for starting or resuming the inferior.  */
  151.  
  152. void
  153. terminal_inferior ()
  154. {
  155.   if (remote_debugging)
  156.     return;
  157.  
  158.   if (terminal_is_ours)   /*  && inferior_thisrun_terminal == 0) */
  159.     {
  160.       fcntl (TTY_FD, F_SETFL, tflags_inferior);
  161.       fcntl (TTY_FD, F_SETFL, tflags_inferior);
  162.       ioctl (TTY_FD, TIOCSETN, &sg_inferior);
  163.  
  164. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  165.       ioctl (TTY_FD, TIOCSETC, &tc_inferior);
  166. #endif
  167. #ifdef TIOCGLTC
  168.       ioctl (TTY_FD, TIOCSLTC, <c_inferior);
  169. #endif
  170. #ifdef TIOCLGET
  171.       ioctl (TTY_FD, TIOCLSET, &lmode_inferior);
  172. #endif
  173.  
  174. #ifdef TIOCGPGRP
  175.       ioctl (TTY_FD, TIOCSPGRP, &pgrp_inferior);
  176. #else
  177.       sigint_ours = (int (*) ()) signal (SIGINT, SIG_IGN);
  178.       sigquit_ours = (int (*) ()) signal (SIGQUIT, SIG_IGN);
  179. #endif /* TIOCGPGRP */
  180.     }
  181.   terminal_is_ours = 0;
  182. }
  183.  
  184. /* Put some of our terminal settings into effect,
  185.    enough to get proper results from our output,
  186.    but do not change into or out of RAW mode
  187.    so that no input is discarded.
  188.  
  189.    After doing this, either terminal_ours or terminal_inferior
  190.    should be called to get back to a normal state of affairs.  */
  191.  
  192. void
  193. terminal_ours_for_output ()
  194. {
  195.   if (remote_debugging)
  196.     return;
  197.  
  198.   terminal_ours_1 (1);
  199. }
  200.  
  201. /* Put our terminal settings into effect.
  202.    First record the inferior's terminal settings
  203.    so they can be restored properly later.  */
  204.  
  205. void
  206. terminal_ours ()
  207. {
  208.   if (remote_debugging)
  209.     return;
  210.  
  211.   terminal_ours_1 (0);
  212. }
  213.  
  214. static void
  215. terminal_ours_1 (output_only)
  216.      int output_only;
  217. {
  218. #ifdef TIOCGPGRP
  219.   /* Ignore this signal since it will happen when we try to set the pgrp.  */
  220.   int (*osigttou) ();
  221. #endif /* TIOCGPGRP */
  222.  
  223.   if (!terminal_is_ours)  /*   && inferior_thisrun_terminal == 0)  */
  224.     {
  225.       terminal_is_ours = 1;
  226.  
  227. #ifdef TIOCGPGRP
  228.       osigttou = (int (*) ()) signal (SIGTTOU, SIG_IGN);
  229.  
  230.       ioctl (TTY_FD, TIOCGPGRP, &pgrp_inferior);
  231.       ioctl (TTY_FD, TIOCSPGRP, &pgrp_ours);
  232.  
  233.       signal (SIGTTOU, osigttou);
  234. #else
  235.       signal (SIGINT, sigint_ours);
  236.       signal (SIGQUIT, sigquit_ours);
  237. #endif /* TIOCGPGRP */
  238.  
  239.       tflags_inferior = fcntl (TTY_FD, F_GETFL, 0);
  240.       ioctl (TTY_FD, TIOCGETP, &sg_inferior);
  241.  
  242. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  243.       ioctl (TTY_FD, TIOCGETC, &tc_inferior);
  244. #endif
  245. #ifdef TIOCGLTC
  246.       ioctl (TTY_FD, TIOCGLTC, <c_inferior);
  247. #endif
  248. #ifdef TIOCLGET
  249.       ioctl (TTY_FD, TIOCLGET, &lmode_inferior);
  250. #endif
  251.     }
  252.  
  253. #ifdef HAVE_TERMIO
  254.   sg_ours.c_lflag |= ICANON;
  255.   if (output_only && !(sg_inferior.c_lflag & ICANON))
  256.     sg_ours.c_lflag &= ~ICANON;
  257. #else /* not HAVE_TERMIO */
  258.   sg_ours.sg_flags &= ~RAW & ~CBREAK;
  259.   if (output_only)
  260.     sg_ours.sg_flags |= (RAW | CBREAK) & sg_inferior.sg_flags;
  261. #endif /* not HAVE_TERMIO */
  262.  
  263.   fcntl (TTY_FD, F_SETFL, tflags_ours);
  264.   fcntl (TTY_FD, F_SETFL, tflags_ours);
  265.   ioctl (TTY_FD, TIOCSETN, &sg_ours);
  266.  
  267. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  268.   ioctl (TTY_FD, TIOCSETC, &tc_ours);
  269. #endif
  270. #ifdef TIOCGLTC
  271.   ioctl (TTY_FD, TIOCSLTC, <c_ours);
  272. #endif
  273. #ifdef TIOCLGET
  274.   ioctl (TTY_FD, TIOCLSET, &lmode_ours);
  275. #endif
  276.  
  277. #ifdef HAVE_TERMIO
  278.   sg_ours.c_lflag |= ICANON;
  279. #else /* not HAVE_TERMIO */
  280.   sg_ours.sg_flags &= ~RAW & ~CBREAK;
  281. #endif /* not HAVE_TERMIO */
  282. }
  283.  
  284. static void
  285. term_status_command ()
  286. {
  287.   register int i;
  288.  
  289.   if (remote_debugging)
  290.     {
  291.       printf_filtered ("No terminal status when remote debugging.\n");
  292.       return;
  293.     }
  294.  
  295.   printf_filtered ("Inferior's terminal status (currently saved by GDB):\n");
  296.  
  297. #ifdef HAVE_TERMIO
  298.  
  299.   printf_filtered ("fcntl flags = 0x%x, c_iflag = 0x%x, c_oflag = 0x%x,\n",
  300.       tflags_inferior, sg_inferior.c_iflag, sg_inferior.c_oflag);
  301.   printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
  302.       sg_inferior.c_cflag, sg_inferior.c_lflag, sg_inferior.c_line);
  303.   printf_filtered ("c_cc: ");
  304.   for (i = 0; (i < NCC); i += 1)
  305.     printf_filtered ("0x%x ", sg_inferior.c_cc[i]);
  306.   printf_filtered ("\n");
  307.  
  308. #else /* not HAVE_TERMIO */
  309.  
  310.   printf_filtered ("fcntl flags = 0x%x, sgttyb.sg_flags = 0x%x, owner pid = %d.\n",
  311.       tflags_inferior, sg_inferior.sg_flags, pgrp_inferior);
  312.  
  313. #endif /* not HAVE_TERMIO */
  314.  
  315. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  316.   printf_filtered ("tchars: ");
  317.   for (i = 0; i < sizeof (struct tchars); i++)
  318.     printf_filtered ("0x%x ", ((char *)&tc_inferior)[i]);
  319.   printf_filtered ("\n");
  320. #endif
  321.  
  322. #ifdef TIOCGLTC
  323.   printf_filtered ("ltchars: ");
  324.   for (i = 0; i < sizeof (struct ltchars); i++)
  325.     printf_filtered ("0x%x ", ((char *)<c_inferior)[i]);
  326.   printf_filtered ("\n");
  327.   ioctl (TTY_FD, TIOCSLTC, <c_ours);
  328. #endif
  329.   
  330. #ifdef TIOCLGET
  331.   printf_filtered ("lmode:  %x\n", lmode_inferior);
  332. #endif
  333. }
  334.  
  335. static void
  336. new_tty (ttyname)
  337.      char *ttyname;
  338. {
  339.   register int tty;
  340.   register int fd;
  341.  
  342. #ifdef TIOCNOTTY
  343.   /* Disconnect the child process from our controlling terminal.  */
  344.   tty = open("/dev/tty", O_RDWR);
  345.   if (tty > 0)
  346.     {
  347.       ioctl(tty, TIOCNOTTY, 0);
  348.       close(tty);
  349.     }
  350. #endif
  351.  
  352.   /* Now open the specified new terminal.  */
  353.  
  354.   tty = open(ttyname, O_RDWR);
  355.   if (tty == -1)
  356.     _exit(1);
  357.  
  358.   /* Avoid use of dup2; doesn't exist on all systems.  */
  359.   if (tty != 0)
  360.     { close (0); dup (tty); }
  361.   if (tty != 1)
  362.     { close (1); dup (tty); }
  363.   if (tty != 2)
  364.     { close (2); dup (tty); }
  365.   if (tty > 2)
  366.     close(tty);
  367. }
  368.  
  369. /* Start an inferior process and returns its pid.
  370.    ALLARGS is a string containing shell command to run the program.
  371.    ENV is the environment vector to pass.  */
  372.  
  373. #ifndef SHELL_FILE
  374. #define SHELL_FILE "/bin/sh"
  375. #endif
  376.  
  377. int
  378. create_inferior (allargs, env)
  379.      char *allargs;
  380.      char **env;
  381. {
  382.   int pid;
  383. #ifndef atarist
  384.   char *shell_command;
  385.   extern int sys_nerr;
  386.   extern char *sys_errlist[];
  387.   extern int errno;
  388.  
  389.   /* If desired, concat something onto the front of ALLARGS.
  390.      SHELL_COMMAND is the result.  */
  391. #ifdef SHELL_COMMAND_CONCAT
  392.   shell_command = (char *) alloca (strlen (SHELL_COMMAND_CONCAT) + strlen (allargs) + 1);
  393.   strcpy (shell_command, SHELL_COMMAND_CONCAT);
  394.   strcat (shell_command, allargs);
  395. #else
  396.   shell_command = allargs;
  397. #endif
  398.  
  399.   /* exec is said to fail if the executable is open.  */
  400.   close_exec_file ();
  401.  
  402. #if defined(USG) && !defined(HAVE_VFORK)
  403.   pid = fork ();
  404. #else
  405.   pid = vfork ();
  406. #endif
  407.  
  408.   if (pid < 0)
  409.     perror_with_name ("vfork");
  410.  
  411.   if (pid == 0)
  412.     {
  413. #ifdef TIOCGPGRP
  414.       /* Run inferior in a separate process group.  */
  415.       setpgrp (getpid (), getpid ());
  416. #endif /* TIOCGPGRP */
  417.  
  418. #ifdef SET_STACK_LIMIT_HUGE
  419.       /* Reset the stack limit back to what it was.  */
  420.       {
  421.     struct rlimit rlim;
  422.  
  423.     getrlimit (RLIMIT_STACK, &rlim);
  424.     rlim.rlim_cur = original_stack_limit;
  425.     setrlimit (RLIMIT_STACK, &rlim);
  426.       }
  427. #endif /* SET_STACK_LIMIT_HUGE */
  428.  
  429.  
  430.       inferior_thisrun_terminal = inferior_io_terminal;
  431.       if (inferior_io_terminal != 0)
  432.     new_tty (inferior_io_terminal);
  433.  
  434. /* It seems that changing the signal handlers for the inferior after
  435.    a vfork also changes them for the superior.  See comments in
  436.    initialize_signals for how we get the right signal handlers
  437.    for the inferior.  */
  438. /* Not needed on Sun, at least, and loses there
  439.    because it clobbers the superior.  */
  440. /*???      signal (SIGQUIT, SIG_DFL);
  441.       signal (SIGINT, SIG_DFL);  */
  442.  
  443.       call_ptrace (0);
  444.       execle (SHELL_FILE, "sh", "-c", shell_command, 0, env);
  445.  
  446.       fprintf (stderr, "Cannot exec %s: %s.\n", SHELL_FILE,
  447.            errno < sys_nerr ? sys_errlist[errno] : "unknown error");
  448.       fflush (stderr);
  449.       _exit (0177);
  450.     }
  451. #ifdef CREATE_INFERIOR_HOOK
  452.   CREATE_INFERIOR_HOOK (pid);
  453. #endif  
  454.  
  455. #else /* atarist */
  456.     /* on the ST we choose a quite different way of starting our inferiors:
  457.         We use Pexec() with the high-bit of mode set, so the started process
  458.         automagically gets traced by us. */
  459.     char startprog[MAXPATHLEN];
  460.     char *args;
  461.     int i;
  462.     extern int inferior_handle;
  463.     extern char inferior_name[64];    
  464.  
  465.     args=allargs+strlen("exec "); /* cut off exec cmd */
  466.     for(i=0; *args && *args!=' ';args++,i++)startprog[i]=*args;
  467.     startprog[i]=0;
  468.  
  469.     for(i=0; i<strlen(startprog);i++)if(startprog[i]=='/')startprog[i]='\\';
  470.     
  471.     while(*(args+1)==' ')args++;
  472.     args[0]=strlen(args)>124 ? 124 : strlen(args);
  473. /*    args[args[0]+1]=0;*/
  474.     
  475.     pid=Pexec(0x8000|100,startprog,args,env);
  476.     if(pid<0)
  477.     {
  478.         fprintf(stderr,"couldn't start program %s, error #%d\n",startprog,pid);
  479.         fflush(stderr);
  480.     }
  481.     else
  482.     {
  483.         inferior_pid=pid; /* inferior_gettxtstrt needs this! */
  484.         
  485.         sprintf(inferior_name,"U:\\PROC\\.%03d",pid);
  486.         inferior_handle=Fopen(inferior_name,2);
  487.         relocate_apropriate_symbols(ADDR_OF_TEXT_SEGMENT);
  488.         relocate_breakpoints(ADDR_OF_TEXT_SEGMENT);
  489.     }
  490. #endif
  491.  
  492.   return pid;
  493. }
  494.  
  495. /* Kill the inferior process.  Make us have no inferior.  */
  496.  
  497. static void
  498. kill_command ()
  499. {
  500.   if (remote_debugging)
  501.     return;
  502.   if (inferior_pid == 0)
  503.     error ("The program is not being run.");
  504.   if (!query ("Kill the inferior process? "))
  505.     error ("Not confirmed.");
  506.   kill_inferior ();
  507. }
  508.  
  509. void
  510. inferior_died ()
  511. {
  512.   inferior_pid = 0;
  513.   attach_flag = 0;
  514.   mark_breakpoints_out ();
  515.   select_frame ((FRAME) 0, -1);
  516.   reopen_exec_file ();
  517.   if (have_core_file_p ())
  518.     set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  519.                       read_pc ()));
  520.   else
  521.     set_current_frame (0);
  522. }
  523.  
  524. #if 0 
  525. /* This function is just for testing, and on some systems (Sony NewsOS
  526.    3.2) <sys/user.h> also includes <sys/time.h> which leads to errors
  527.    (since on this system at least sys/time.h is not protected against
  528.    multiple inclusion).  */
  529. static void
  530. try_writing_regs_command ()
  531. {
  532. #ifndef atarist
  533.   register int i;
  534.   register int value;
  535.   extern int errno;
  536.  
  537.   if (inferior_pid == 0)
  538.     error ("There is no inferior process now.");
  539.  
  540.   /* A Sun 3/50 or 3/60 (at least) running SunOS 4.0.3 will have a
  541.      kernel panic if we try to write past the end of the user area.
  542.      Presumably Sun will fix this bug (it has been reported), but it
  543.      is tacky to crash the system, so at least on SunOS4 we need to
  544.      stop writing when we hit the end of the user area.  */
  545.   for (i = 0; i < sizeof (struct user); i += 2)
  546.     {
  547.       QUIT;
  548.       errno = 0;
  549.       value = call_ptrace (3, inferior_pid, i, 0);
  550.       call_ptrace (6, inferior_pid, i, value);
  551.       if (errno == 0)
  552.     {
  553.       printf (" Succeeded with address 0x%x; value 0x%x (%d).\n",
  554.           i, value, value);
  555.     }
  556.       else if ((i & 0377) == 0)
  557.     printf (" Failed at 0x%x.\n", i);
  558.     }
  559. #endif /* atarist */
  560. }
  561. #endif
  562.  
  563. void
  564. _initialize_inflow ()
  565. {
  566.   add_com ("term-status", class_obscure, term_status_command,
  567.        "Print info on inferior's saved terminal status.");
  568.  
  569. #if 0
  570.   add_com ("try-writing-regs", class_obscure, try_writing_regs_command,
  571.        "Try writing all locations in inferior's system block.\n\
  572. Report which ones can be written.");
  573. #endif
  574.  
  575.   add_com ("kill", class_run, kill_command,
  576.        "Kill execution of program being debugged.");
  577.  
  578.   inferior_pid = 0;
  579.  
  580.   ioctl (TTY_FD, TIOCGETP, &sg_ours);
  581.   fcntl (TTY_FD, F_GETFL, tflags_ours);
  582.  
  583. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  584.   ioctl (TTY_FD, TIOCGETC, &tc_ours);
  585. #endif
  586. #ifdef TIOCGLTC
  587.   ioctl (TTY_FD, TIOCGLTC, <c_ours);
  588. #endif
  589. #ifdef TIOCLGET
  590.   ioctl (TTY_FD, TIOCLGET, &lmode_ours);
  591. #endif
  592.  
  593. #ifdef TIOCGPGRP
  594.   ioctl (TTY_FD, TIOCGPGRP, &pgrp_ours);
  595. #endif /* TIOCGPGRP */
  596.  
  597.   terminal_is_ours = 1;
  598. }
  599.  
  600.